package org.crmsystem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.crmsystem.dao.ProKindMapper;
import org.crmsystem.model.*;
import org.crmsystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/10.
 */
@Controller

@RequestMapping("/promsg")
public class ProMsgController {

    private static Logger logger = LogManager.getLogger(ProKindMapper.class);

    @Autowired
    private IProductService productService;
    @Autowired
    private IProKindService proKindService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IOrgService orgService;



    /**
     * 返回所有页面
     *
     * @return the model and view
     */
    @RequestMapping("/allpro")

    public ModelAndView selectProByPage() {



        ModelAndView allpro = new ModelAndView("proMsg");


        return allpro;

    }
    /**
     * 返回所有
     *
     * @return the model and view
     */
    @RequestMapping(value = "/allproData",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectProBy(Product product,int pageSize,int currPage) {

        logger.info(pageSize+"ddd");
        Map map=new HashMap();
        map.put("pageSize", pageSize);
        map.put("currPage", (currPage-1)*pageSize);
        map.put("product", product);
        List listPro = productService.selectByTerms(map);

        JSON json=new JSONArray(listPro);



        return json.toJSONString();

    }

    /**
     * 根据id查询产品
     *
     * @return the model and view
     */
    @RequestMapping("/proById")
    public ModelAndView selectProById(@RequestParam int id) {

       /* Product p=new Product();
        p.setId(id);
        Map m = new HashMap();
        m.put("product",p);
        List product1 = productService.selectByTerms(m);*/


        List<Map> listPro = productService.select();

        Map ssmMap = new HashMap<>();
        for (Map m:listPro
             ) {
            if ((int)m.get("id")==id){
                ssmMap.put("ssm", m);
            }
        }
        return new ModelAndView("simpleProMsg", ssmMap);

    }

    /**
     * 新增产品类型页面
     *
     * @return the model and view
     */
    @RequestMapping("/insertKindPage")
    @ResponseBody
    public ModelAndView insertPage() {

        return new ModelAndView("proKindAdd");
    }

    /**
     * 插入产品类型
     *
     * @return the model and view
     */
    @RequestMapping("/insertKind")
    @ResponseBody
    public Map insert(ProKind proKind) {

        logger.info(proKind.getName() + "jjjjjjjjjjjjj");
        int flag = proKindService.insert(proKind);
        Map map = new HashMap();

        if (flag == 0) {
            map.put("flag", null);

        } else {
            map.put("flag", 1);

        }
        return map;
    }

    /**
     * 新增产品页面
     *
     * @return the model and view
     */
    @RequestMapping("/insertProPage")
    public ModelAndView insertPro() {

        List<ProKind> proKinds = proKindService.select();
        Map map1 = new HashMap();
        User user = new User();
        map1.put("user", user);
        map1.put("pageSize", 100);
        map1.put("currPage", 1);
        Page page=userService.getUser(map1);
        List<Object> contacts = page.getResult();
        List<OrgInfo> orgInfos = orgService.selectOrgList(null);
        Map<String, Object> map = new HashMap<>();

        map.put("prokind", proKinds);

        map.put("contact", contacts);
        map.put("operator", contacts);
        map.put("orgName", orgInfos);
        return new ModelAndView("proAdd", map);
    }

    /**
     * 新增产品
     *
     * @return the model and view
     */
    @RequestMapping("/insertpro")
    @ResponseBody
    public String insert(Product product) {

        logger.info(product.getProName() + "jjjjj");
        logger.info(product.getContactId() + "jjjjj");
        logger.info(product.getKindId() + "jjjjj");
        logger.info(product.getProState() + "jjjjj");
        logger.info(product.getUseDate() + "jjjjj");
        int flag = productService.inserts(product);

        Map map=new HashMap();
        if (flag == 0) {
            return null;
        }
        return "1";
    }

    /**
     * 删除产品
     *
     * @param
     * @return the int
     */
    @RequestMapping("/delById")
    @ResponseBody
    public String del(Product product) {
        int flag = productService.deleteByPrimaryKey(product.getId());
        String msg = null;
        logger.info(flag + "======");
        if (flag == 1) {
            msg = "SUCC";
        } else {
            msg = "";
        }
        return msg;

    }



}
